<template>
  <el-dialog
    :title="!dataForm.superMemberId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="超级会员名称" prop="spuerMemberName">
      <el-input v-model="dataForm.spuerMemberName" placeholder="超级会员名称"></el-input>
    </el-form-item>
    <el-form-item label="背景色" prop="backgroundColor">
      <el-input v-model="dataForm.backgroundColor" placeholder="背景色"></el-input>
    </el-form-item>
    <el-form-item label="图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="图标"></el-input>
    </el-form-item>
    <el-form-item label="背景图" prop="backgroundImage">
      <el-input v-model="dataForm.backgroundImage" placeholder="背景图"></el-input>
    </el-form-item>
    <el-form-item label="超级会员消费额" prop="superMemberPrice">
      <el-input v-model="dataForm.superMemberPrice" placeholder="超级会员消费额"></el-input>
    </el-form-item>
    <el-form-item label="普通会员佣金" prop="ordinaryMemberCommission">
      <el-input v-model="dataForm.ordinaryMemberCommission" placeholder="普通会员佣金"></el-input>
    </el-form-item>
    <el-form-item label="自购省钱" prop="oneselfBuyMoney">
      <el-input v-model="dataForm.oneselfBuyMoney" placeholder="自购省钱"></el-input>
    </el-form-item>
    <el-form-item label="直推评级佣金" prop="directlyCommission">
      <el-input v-model="dataForm.directlyCommission" placeholder="直推评级佣金"></el-input>
    </el-form-item>
    <el-form-item label="权益说明" prop="benefitsContent">
      <el-input v-model="dataForm.benefitsContent" placeholder="权益说明"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          superMemberId: 0,
          spuerMemberName: '',
          backgroundColor: '',
          icon: '',
          backgroundImage: '',
          superMemberPrice: '',
          ordinaryMemberCommission: '',
          oneselfBuyMoney: '',
          directlyCommission: '',
          benefitsContent: ''
        },
        dataRule: {
          spuerMemberName: [
            { required: true, message: '超级会员名称不能为空', trigger: 'blur' }
          ],
          backgroundColor: [
            { required: true, message: '背景色不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '图标不能为空', trigger: 'blur' }
          ],
          backgroundImage: [
            { required: true, message: '背景图不能为空', trigger: 'blur' }
          ],
          superMemberPrice: [
            { required: true, message: '超级会员消费额不能为空', trigger: 'blur' }
          ],
          ordinaryMemberCommission: [
            { required: true, message: '普通会员佣金不能为空', trigger: 'blur' }
          ],
          oneselfBuyMoney: [
            { required: true, message: '自购省钱不能为空', trigger: 'blur' }
          ],
          directlyCommission: [
            { required: true, message: '直推评级佣金不能为空', trigger: 'blur' }
          ],
          benefitsContent: [
            { required: true, message: '权益说明不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.superMemberId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.superMemberId) {
            this.$http({
              url: this.$http.adornUrl(`/system/suppermember/info/${this.dataForm.superMemberId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.spuerMemberName = data.supperMember.spuerMemberName
                this.dataForm.backgroundColor = data.supperMember.backgroundColor
                this.dataForm.icon = data.supperMember.icon
                this.dataForm.backgroundImage = data.supperMember.backgroundImage
                this.dataForm.superMemberPrice = data.supperMember.superMemberPrice
                this.dataForm.ordinaryMemberCommission = data.supperMember.ordinaryMemberCommission
                this.dataForm.oneselfBuyMoney = data.supperMember.oneselfBuyMoney
                this.dataForm.directlyCommission = data.supperMember.directlyCommission
                this.dataForm.benefitsContent = data.supperMember.benefitsContent
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/system/suppermember/${!this.dataForm.superMemberId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'superMemberId': this.dataForm.superMemberId || undefined,
                'spuerMemberName': this.dataForm.spuerMemberName,
                'backgroundColor': this.dataForm.backgroundColor,
                'icon': this.dataForm.icon,
                'backgroundImage': this.dataForm.backgroundImage,
                'superMemberPrice': this.dataForm.superMemberPrice,
                'ordinaryMemberCommission': this.dataForm.ordinaryMemberCommission,
                'oneselfBuyMoney': this.dataForm.oneselfBuyMoney,
                'directlyCommission': this.dataForm.directlyCommission,
                'benefitsContent': this.dataForm.benefitsContent
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
